/*
 * Created on 19. des.. 2006
 * Created by Tomas
 */
package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import people.Person;
import placing.PlaceNotFoundException;

public class PersonsDetailPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private Presenter parent;
	private Person person;
	
	private String 
	nameString 			= "Name",
	nameContent		 	= "Name",
	mailString 			= "e-mail", 
	currentPlaceString 	= "Current place",
	preffPlaceString 	= "Preffered place", 
	scoreString 		= "Score",
	commentString 		= "Comment", 
	illegalString 		= "undefined";
	
	JLabel nameLabel = new JLabel(nameString);
	JLabel mailLabel = new JLabel(mailString);
	JLabel currentLabel = new JLabel(currentPlaceString);
	JLabel preffLabel = new JLabel(preffPlaceString);
	JLabel scoreLabel = new JLabel(scoreString);
	JLabel commLabel = new JLabel(commentString);
	
	JTextField nameField = new JTextField(nameContent, 10);
	JTextField mailField = new JTextField(mailString, 15);
	JTextField currentField = new JTextField(currentPlaceString, 5);
	JTextField preffField = new JTextField(preffPlaceString, 5);
	JTextField scoreField = new JTextField(scoreString, 5);
	JTextArea commentArea = new JTextArea(commentString, 30, 4);
	JScrollPane commentPane = new JScrollPane(commentArea,
			ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
			ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	
	JButton saveButton = new JButton("Save");
	JButton deleteButton = new JButton("Delete");
	JButton newButton = new JButton("New");
	JButton cancelButton = new JButton("Cancel");
	
	
	public PersonsDetailPanel(Presenter pres) {
		super(new GridLayout(0, 1));
		parent = pres;

		//Layout
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.ipadx = 2;
		c.ipady = 2;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(2, 2, 2, 2);
		GridBagConstraints cLeft = (GridBagConstraints)c.clone();
		GridBagConstraints cRight = (GridBagConstraints)c.clone();
		GridBagConstraints cFill = (GridBagConstraints)c.clone();
		
		cLeft.gridy = 0;
		cLeft.gridx = 0;
		cLeft.weightx = 0;
		
		cRight.gridy = 0;
		cRight.gridx = 1;
		cRight.weightx = 1;
		
		cFill.gridx = 0;
		cFill.gridwidth = 2;
		cFill.weighty = 1;
		cFill.fill = GridBagConstraints.BOTH;

		//Personalia
		JPanel p = new JPanel(gridbag);
		p.add(nameLabel, cLeft);
		p.add(nameField, cRight);
		
		cLeft.gridy++;
		cRight.gridy++;
		p.add(mailLabel, cLeft);
		p.add(mailField, cRight);
		
		//Score and place
		cLeft.gridy++;
		cRight.gridy++;
		p.add(scoreLabel, cLeft);
		p.add(scoreField, cRight);
		
		cLeft.gridy++;
		cRight.gridy++;
		p.add(currentLabel, cLeft);
		p.add(currentField, cRight);
		
		cLeft.gridy++;
		cRight.gridy++;
		p.add(preffLabel, cLeft);
		p.add(preffField, cRight);
		
		
		//Comment
		cLeft.gridy++;
		cRight.gridy++;
		p.add(commLabel, cLeft);
		cLeft.gridy++;
		cRight.gridy++;
		cFill.gridy = cLeft.gridy;
		p.add(commentPane, cFill);
		
		//Buttons
		newButton.addActionListener(this);
		
		//Comment is high, go over a row
		cFill.gridy++;
		cFill.gridheight = 1;
		cFill.fill = GridBagConstraints.HORIZONTAL;
		cFill.anchor = GridBagConstraints.PAGE_END;
		
		JPanel buttons = new JPanel(new GridLayout(0, 2, 2, 2));
		buttons.add(newButton);
		buttons.add(deleteButton);
		
		buttons.add(cancelButton);
		buttons.add(saveButton);
		
		p.add(buttons, cFill);
		
		//Put it all together
		add(p);
	}
	
	protected void present(Person p) {
		person = p;
		nameField.setText(person.getName());
		mailField.setText(person.getEmail());
		try {
			currentField.setText(person.getCurrentPlace().toString());
		} catch (PlaceNotFoundException e) {
			currentField.setText(illegalString);
		}
		try {
			preffField.setText(person.getPreferredPlace().toString());
		} catch (NullPointerException e) {
			preffField.setText(illegalString);
		}
		scoreField.setText(""+person.getScore());
		commentArea.setText(person.getComment());
	}

	/**
	 * A button has been pressed.
	 * @param e
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == newButton) {
			//TODO Create a new, empty person
		}
		else {
			if (e.getSource() == cancelButton) {
				//cancel all changes, reverting to the stored Person
				present(person);
			} else {
				if (e.getSource() == deleteButton) {
					//TODO delete the person
				} else {
					//TODO save changes to the person
				}
			}
			
		}
	}
	
	/*
	 * After changing the person - tell the Presenter that it's been changed
	 */
	private void tellPresenter() {
		parent.updateModels();
	}

}
